package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.acticle.domain.Answer;
import cn.wolfcode.luowowo.acticle.domain.AnswerContent;
import cn.wolfcode.luowowo.acticle.domain.Query;
import cn.wolfcode.luowowo.acticle.query.AnswerQuery;
import cn.wolfcode.luowowo.acticle.service.IAnswerService;
import cn.wolfcode.luowowo.acticle.vo.AnswerStatisVO;
import cn.wolfcode.luowowo.acticle.vo.UserCommentVO;
import cn.wolfcode.luowowo.article.mapper.AnswerContentMapper;
import cn.wolfcode.luowowo.article.mapper.AnswerMapper;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class AnswerServiceImpl implements IAnswerService{

    @Autowired
    private AnswerMapper answerMapper;

    @Autowired
    private AnswerContentMapper answerContentMapper;


    @Override
    public Answer getAnswerByThumbupnum(Long id) {
        return answerMapper.selectByThumbupnum(id);
    }

    @Override
    public PageInfo getAnswerByqo(AnswerQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        return new PageInfo(answerMapper.selectByQueryId(qo));
    }

    @Override
    public AnswerContent getContentByAnswer(Long id) {
        return answerContentMapper.selectByid(id);
    }

    @Override
    public void saveAnswer(Answer answer) {
        String content = answer.getAnswerContent().getContent();
        if(content.length() > 80){
            answer.setSummary(content.substring(0,80));
        }else{
            answer.setSummary(content);
        }
        answer.setCreateTime(new Date());
        answer.setCommentnum(0);
        answer.setThumbupnum(0);
        answer.setFavornum(0);
        answer.setGold(false);
        answerMapper.insertByAnswer(answer);

        AnswerContent answerContent = new AnswerContent();
        answerContent.setId(answer.getId());
        answerContent.setContent(content);
        answerContentMapper.insert(answerContent);

    }

    @Override
    public Answer get(Long aid) {
        return answerMapper.selectByPrimaryKey(aid);
    }

    @Override
    public void updateStatisByVo(AnswerStatisVO vo1) {
        answerMapper.updateStatisByVo(vo1);
    }
    @Override
    public List<Answer> list() {
        return answerMapper.selectAll();
    }
}
